// pages/discuss/discuss.js
const appdata = getApp()
const AV = getApp().globalData.AV
Page({
  data: {
    cover:[], //上传图片
    val: '', // 评分
    textarea: '', // 上传文章
    listdetailsid: '',
    openid: '', // user凭证
    vids: '', // 购物车id
    list: [],
    username: '',
    userimg: ''
  },
  upload() { // 上传
    var that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      success(res) {
        var localFileUri = res.tempFiles[0].tempFilePath
        var fileName = res.tempFiles[0].tempFilePath.split('/')[3]
        const data = { blob: { uri: localFileUri } }
        const file = new AV.File(fileName, data)
        file.save().then((file) => {
          that.data.cover.push(file.url())
          that.setData({
            cover:  that.data.cover
          })
        });
      }
    })
  },
  pingfen(e){ //评分
    console.log(e.currentTarget.dataset.val);
    this.setData({
      val: e.currentTarget.dataset.val
    })
  },
  deleimg(e){ // 删除图片
    this.data.cover.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      cover: this.data.cover
    })
  },
  bindTextAreaBlur(e){ // 文本框双向绑定
    this.setData({
      textarea: e.detail.value
    })
  },
  gouwuche(){ // 提交
    const Todo = AV.Object.extend('discuss');
    const todo = new Todo();
    todo.set('discusimg', this.data.cover);
    todo.set('discussname', this.data.textarea);
    todo.set('listdetailsid', this.data.listdetailsid);
    todo.set('grade', this.data.val);
    todo.set('openid', this.data.openid);
    todo.set('username', this.data.username);
    todo.set('userimg', this.data.userimg);
    todo.save().then((todo) => {
      wx.switchTab({
        url: "/pages/my/my",
        success(res) { 
          wx.showToast({
            title: "已评论"
          })
        }
      })
    })
  },
  tijiao(){ // id购物车改变状态为8
    const todo = AV.Object.createWithoutData('cart',  this.data.vids);
    todo.set('stater', '7');
    todo.save().then((todo) => {
      this.gouwuche()
    })
  },
  onLoad(options) {
    this.setData({
      listdetailsid: options.listid,
      vids: options.id
    })
    console.log(options.listid)
    const query = new AV.Query('detail');
    query.equalTo('stater', '0');
    query.equalTo('objectId', options.listid);
    query.find().then((students) => {
      this.setData({
        list: students
      })
      console.log(students);
    })
  },
  onShow() {
    if (appdata.globalData.userInfo == null) {
      console.log("未登录");
    } else {
      const openid = appdata.globalData.userInfo
      console.log(openid);
      this.setData({
        openid: openid
      })
      const query = new AV.Query('users');
    //   query.equalTo('stater', '0');
      query.equalTo('openId', openid);
      query.find().then((students) => {
        console.log(students);
        this.setData({
          username: students[0].attributes.username,
          userimg: students[0].attributes.userimg
        })
      })
    }
  }
})